package chapter15;

import java.util.concurrent.TimeUnit;

/**
 * @author lzy
 * @version v1.0
 * Create DateTime: 2019/2/23 13:49
 */
class Racer2 extends Thread {
    MyLatch latch;

    public Racer2(MyLatch latch) {
        this.latch = latch;
    }

    @Override
    public void run() {
        try {
            this.latch.await();
            System.out.println("start run " + Thread.currentThread().getName());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

public class RacerWithLatchDemo {
    public static void main(String[] args) throws InterruptedException {
        int num = 10;
        MyLatch latch = new MyLatch(1);
        Thread[] racers = new Thread[num];
        for (int i = 0; i < num; i++) {
            racers[i] = new Racer2(latch);
            racers[i].start();
        }

        TimeUnit.SECONDS.sleep(3);
        latch.countDown();
    }
}
